<?php

namespace app\admin\model\kuerp;

use think\Model;
use traits\model\SoftDelete;

class StaffSignIn extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'kuerp_staff_sign_in';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        // 'working_time_text'
        'file_ids_arr'
    ];
    

    



    public function getWorkingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['working_time']) ? $data['working_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setWorkingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getFileIdsArrAttr($value, $data) {
		$files  = !empty($data['file_ids']) ? explode(',', $data['file_ids']) : [];
		$result = [];
		foreach ($files as $fid) {
			if ($fid) {
				$result[] = cdnurl(File::getUrl($fid), true);
			}
		}
		return $result;
	}

    public function customers()
    {
        return $this->belongsTo('Customers', 'customer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function staffmember()
    {
        return $this->belongsTo('StaffMember', 'staff_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    //员工
	public function staff() {
		return $this->belongsTo('StaffMember', 'staff_id', 'id')->field('id,name,admin_id');
	}
}
